package com.grechur.wanandroidcompose.viewmodel

import android.app.Application
import androidx.compose.runtime.*
import androidx.lifecycle.AndroidViewModel
import com.grechur.wanandroidcompose.bean.project.ProjectBean
import com.grechur.wanandroidcompose.bean.project.ProjectInfo
import com.grechur.wanandroidcompose.http.NetworkRequest
import com.grechur.wanandroidcompose.http.service.ProjectService

class ProjectViewModel(application: Application) : AndroidViewModel(application){
//    val mRepository: ProjectRepository = ProjectRepository()
//    直接将传回的数据给页面，页面需要处理的逻辑较多，不太适合
//    val projectTitles = mRepository.getProjectTitles()

//    直接使用列表的方式，不太好处理异常情况，需要多一个字段来显示
//    var mProjectTitles by mutableStateOf(listOf<ProjectInfo>())
//        private set

    var mProjectResultTitles by mutableStateOf(Result.success(listOf<ProjectInfo>()))
        private set

    suspend fun getNetProjectTitles() {
        try {
            val projectTitles = NetworkRequest.getService(getApplication(), ProjectService::class.java).getProjectTitles()
            projectTitles.takeIf { it != null }?.let { projectBean: ProjectBean ->
                if(projectBean.errorCode == 0) {
                    projectBean.data.takeIf { !it.isNullOrEmpty() }?.let { list:List<ProjectInfo>->
//                        mProjectTitles = list
                        mProjectResultTitles = Result.success(list)
                    }
                } else {
                    
                }
            }
        } catch (e:Exception) {
            mProjectResultTitles = Result.failure(e)
        }
    }



//    fun getProjectInfos(index:Int = 1,cid:String?): LiveData<Result<ArticlesBean>> {
//
//        return mRepository.getProjectArticles(index,cid)
//    }

}

